package vip.xiaonuo.supplier.modular.warehouse.storeslocation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.entity.SupplierStoresLocation;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.mapper.SupplierStoresLocationMapper;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.param.SupplierStoresLocationAddParam;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.param.SupplierStoresLocationEditParam;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.param.SupplierStoresLocationIdParam;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.param.SupplierStoresLocationPageParam;
import vip.xiaonuo.supplier.modular.warehouse.storeslocation.service.SupplierStoresLocationService;

import java.util.List;

/**
 * 库位信息Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/15 21:29
 **/
@Service
public class SupplierStoresLocationServiceImpl extends ServiceImpl<SupplierStoresLocationMapper, SupplierStoresLocation> implements SupplierStoresLocationService {

    @Override
    public Page<SupplierStoresLocation> page(SupplierStoresLocationPageParam supplierStoresLocationPageParam) {
        QueryWrapper<SupplierStoresLocation> queryWrapper = new QueryWrapper<SupplierStoresLocation>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierStoresLocationPageParam.getLocationCode())) {
            queryWrapper.lambda().like(SupplierStoresLocation::getLocationCode, supplierStoresLocationPageParam.getLocationCode());
        }
        if(ObjectUtil.isNotEmpty(supplierStoresLocationPageParam.getLocationName())) {
            queryWrapper.lambda().like(SupplierStoresLocation::getLocationName, supplierStoresLocationPageParam.getLocationName());
        }
        if(ObjectUtil.isNotEmpty(supplierStoresLocationPageParam.getLocationStore())) {
            queryWrapper.and(i -> i.lambda().eq(SupplierStoresLocation::getLocationStore, supplierStoresLocationPageParam.getLocationStore())
                    .or().eq(SupplierStoresLocation::getLocationStorage, supplierStoresLocationPageParam.getLocationStore()));
        }
        if(ObjectUtil.isNotEmpty(supplierStoresLocationPageParam.getSearchKey())){
            queryWrapper.and(i -> i.lambda().like(SupplierStoresLocation::getLocationName, supplierStoresLocationPageParam.getSearchKey())
                    .or().like(SupplierStoresLocation::getLocationCode, supplierStoresLocationPageParam.getSearchKey()));
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierStoresLocation::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierStoresLocationPageParam.getSortField(), supplierStoresLocationPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierStoresLocationPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierStoresLocationPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierStoresLocationPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierStoresLocation::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierStoresLocationAddParam supplierStoresLocationAddParam) {
        SupplierStoresLocation supplierStoresLocation = BeanUtil.toBean(supplierStoresLocationAddParam, SupplierStoresLocation.class);
        //数据归属
        supplierStoresLocation.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierStoresLocation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierStoresLocationEditParam supplierStoresLocationEditParam) {
        SupplierStoresLocation supplierStoresLocation = this.queryEntity(supplierStoresLocationEditParam.getId());
        BeanUtil.copyProperties(supplierStoresLocationEditParam, supplierStoresLocation);
        this.updateById(supplierStoresLocation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierStoresLocationIdParam> supplierStoresLocationIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierStoresLocationIdParamList, SupplierStoresLocationIdParam::getId));
    }

    @Override
    public SupplierStoresLocation detail(SupplierStoresLocationIdParam supplierStoresLocationIdParam) {
        return this.queryEntity(supplierStoresLocationIdParam.getId());
    }

    @Override
    public SupplierStoresLocation queryEntity(String id) {
        SupplierStoresLocation supplierStoresLocation = this.getById(id);
        if(ObjectUtil.isEmpty(supplierStoresLocation)) {
            throw new CommonException("库位信息不存在，id值为：{}", id);
        }
        return supplierStoresLocation;
    }
}
